package com.xgj.ph.dao.impl;

import com.xgj.ph.dao.CommunityDao;
import com.xgj.ph.entity.Community;
import com.xgj.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class CommunityDaoImpl implements CommunityDao {
    @Override
    public int insert(Community community) {
        String sql = "insert into community values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object obj[] = {
                community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                community.getCreateTime(),
                community.getCommunityShopCount(),
                community.getCommunityAreaCode(),
                community.getCommunityCoordinates(),
                community.getCommunityStatus(),
                community.getCommunityUserCount(),
                community.getDistance()
        };
        return JDBCDruidUtil.update(sql,obj);

    }

    @Override
    public int update(Community community) {
        String sql = "update community set communityName=?,communityProvince=?,communityCity=?,communityDistrict=?,communityAddress=?,communityLatitude=?,communityLongitude=?  where communityId = ?";
        Object obj[] = {
                community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                community.getCommunityId()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int updateShopCount(int communityId, int num) {
        String sql = "update community set communityShopCount=communityShopCount+? where communityId = ?";
        Object obj[] = {
                num,
                communityId
        };
        return JDBCDruidUtil.update(sql,obj);
    }


    @Override
    public int updateShop(int communityId, int shopId) {
        String sql = "update shop set shopCommunityId=? where shopId = ?";
        Object obj[]={
                communityId,
                shopId
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int delete(int communityId) {
        String sql = "delete from community where communityId = ?";
        Object obj[] = {communityId};
        return JDBCDruidUtil.update(sql,obj);
    }

    //  map：保存的数据 communityName 。。 starttime 。。 endTime 。。 pageNum .. pageSize 。。
    @Override
    public List<Community> selectAll(Map<String, String> map) {
        List<Community> list = new ArrayList<Community>();
        //  分页查询    模糊查询    时间范围查询  --  动态sql
        String sql = "select * from community where 1=1 ";
        List<Object> params = new ArrayList<>();
        //  判断 如果map集合中有communityName  则该sql需要进行模糊查询
        String communityName = map.get("communityName");
        if (communityName != null && !"".equals(communityName)) {     //  小区名部位空字符串和null
            sql+=" and communityName like ? ";
            params.add("%"+communityName+"%");
        }
        //  判断是否需要进行日期范围查询
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql+=" and createTime between ? and ? ";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        //  排序
        sql+=" order by communityId desc";
        //  判断是否有分页操作
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql+=" limit ?,? ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        //  查询
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if(!res.next())break;
                Community community = new Community(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getDate(9),
                        res.getInt(10),
                        res.getInt(11),
                        res.getString(12),
                        res.getInt(13),
                        res.getInt(14),
                        res.getString(15)
                );
                list.add(community);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        String sql = "select count(1) from community where 1=1 ";

        ResultSet res = JDBCDruidUtil.query(sql,null);
        int count = 0;
        try {
            if(res.next())
                count = res.getInt(1);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        JDBCDruidUtil.close();
        return count;
    }


}
